<template>
  <div class="el_card_main">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-input placeholder="根据用户名搜索" v-model="input" clearable>
        </el-input>
        <el-button style="padding: 3px 0" @click="clearContent">清空</el-button>
        <el-button style="padding: 3px 0" type="primary">搜索</el-button>

        <el-button
          style="float: right; padding: 3px 0"
          icon="el-icon-edit"
          type="success"
          @click="centerDialogVisible = true"
          >新增用户</el-button
        >
      </div>
      <el-alert :title="title" type="info" show-icon :closable="closable">
      </el-alert>
      <!-- 用户列表 -->
      <div class="text item">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            prop="id"
            label="序号"
            width="180"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="180"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="phone"
            label="联系电话"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="username"
            label="用户名"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="permission_group_title"
            label="权限组名称"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="role"
            label="角色"
            header-align="center"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" header-align="center" align="center">
            <template slot-scope="scope" v-if="scope.row.role !== 'admin'">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                plain
                circle
                style="width: 36px; height: 36px"
                @click="handleEdit(scope.row)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                plain
                circle
                style="width: 36px; height: 36px"
                @click="handleDelete(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 新增用户表 -->
      <el-dialog
        title="创建用户"
        :visible.sync="centerDialogVisible"
        width="951.5px"
        center
      >
        <el-form
          label-position="right"
          :rules="rules"
          ref="form"
          :model="form"
          label-width="80px"
          style="width: 400px; margin-left: 120px"
          class="addForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email"></el-input> </el-form-item
          ><el-form-item label="密码" prop="password">
            <el-input v-model="form.password"></el-input> </el-form-item
          ><el-form-item label="角色">
            <el-input v-model="form.role"></el-input> </el-form-item
          ><el-form-item label="权限组名">
            <el-select
              v-model.number="form.permission_group_id"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone"></el-input> </el-form-item
          ><el-form-item label="介绍">
            <el-input
              v-model="form.introduction"
              type="textarea"
              :rows="2"
              size="small"
            ></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button
            @click="centerDialogVisible = false"
            style="padding: 3px 0; width: 74px; height: 36px; font-size: 14px"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="submitForm('form')"
            addPerson
            style="padding: 3px 0; width: 74px; height: 36px; font-size: 14px"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 新增用户表 -->

      <!-- 编辑成员表 -->
      <el-dialog
        title="编辑用户"
        :visible.sync="dialogVisible"
        width="951.5px"
        center
      >
        <el-form
          label-position="right"
          :rules="rules"
          ref="form"
          :model="form"
          label-width="80px"
          style="width: 400px; margin-left: 120px"
          class="addForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email"></el-input> </el-form-item
          ><el-form-item label="角色">
            <el-input v-model="form.role"></el-input> </el-form-item
          ><el-form-item label="权限组名">
            <el-select
              v-model.number="form.permission_group_id"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone"></el-input> </el-form-item
          ><el-form-item label="介绍">
            <el-input
              v-model="form.introduction"
              type="textarea"
              :rows="2"
              size="small"
            ></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button
            @click="closeDialog"
            style="padding: 3px 0; width: 74px; height: 36px; font-size: 14px"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="updateForm()"
            addPerson
            style="padding: 3px 0; width: 74px; height: 36px; font-size: 14px"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 编辑成员表 -->

      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          layout="prev, pager, next, sizes,jumper"
          :total="total"
          :current-page="queryObj.page"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="queryObj.pagesize"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, update, detail } from '@/api/base/users.js'
export default {
  data () {
    return {
      input: '',
      value: false,
      centerDialogVisible: false,
      dialogVisible: false,
      dialogFormVisible: false,
      closable: false,
      userid: '',
      total: 0,
      options: [
        {
          value: 1,
          label: '超级管理员组'
        },
        {
          value: 2,
          label: '编辑组'
        },
        {
          value: 3,
          label: '昆昆'
        }
      ],
      // 新增用户表单
      form: {
        username: '',
        email: '',
        phone: '',
        password: '',
        role: '',
        permission_group_id: '',
        introduction: ''
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 11, message: '长度在 6 到 11 个字符', trigger: 'blur' }
        ]
      },
      queryObj: {
        page: 1,
        pagesize: 10
      },
      tableData: []
    }
  },

  async created () {
    this.renderTable()
  },

  methods: {
    // 清空事件
    clearContent () {
      this.input = ''
    },
    // 渲染表单
    async renderTable () {
      const { data } = await list(this.queryObj)
      this.total = data.counts
      this.tableData = [...data.list]
    },
    // 添加成员
    async submitForm (formName) {
      try {
        const valid = await this.$refs[formName].validate()
        if (!valid) return
        const res = await add(this.form)
        console.log(res)
        this.tableData = []
        this.renderTable()
        this.centerDialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑成员
    async handleEdit (row) {
      this.dialogVisible = true
      this.userid = row.id
      // 封装成对象 获取用户详情
      const datas = {
        id: row.id
      }
      const { data } = await detail(datas)
      // 将数据传给表单
      this.form.username = data.username
      this.form.email = data.email
      this.form.introduction = data.introduction
      this.form.permission_group_id = data.permission_group_id
      this.form.role = data.role
      this.form.phone = data.phone
      console.log(this.form)
    },
    // 删除成员
    async handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = {
            id: row.id
          }
          const res = await remove(data)
          console.log(res)

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.renderTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 更新成员
    async updateForm () {
      const res = await update(this.form)
      console.log(res)
      this.closeDialog()
    },
    // 关闭弹窗,并重置表单
    closeDialog () {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 页数变化时触发
    handlePageChange (pageNum) {
      this.queryObj.page = pageNum
      this.renderTable()
    },
    // 每页显示信息条数
    handleSizeChange (pageSize) {
      this.queryObj.pagesize = pageSize
      this.renderTable()
    }
  },
  computed: {
    // 消息记录
    title () {
      return `共 ${this.total} 条记录`
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  width: 1681px;
  margin: 0 auto;
  padding: 20px;
  margin-top: 20px;
  .el-input {
    width: 200px;
    height: 32px;
  }
  .addForm {
    .el-input {
      width: 280px;
      height: 36px;
    }
  }
  .el-button--default {
    width: 56px;
    height: 32px;
    margin: 0 0 0 15px;
  }
  .el-alert--info {
    margin-bottom: 20px;
  }
  .el-button--primary {
    width: 56px;
    height: 32px;
  }
  .el-button--success {
    width: 97px;
    height: 32px;
    font-size: 12px;
  }
  .text {
    font-size: 14px;
  }
  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: '';
  }
  .clearfix:after {
    clear: both;
  }
}
</style>
